import { bindVehicleAPI, fetchChauffeurAPI, fetchChauffeurDetailAPI, fetchLicenseInfoAPI, fetchVehicleAllAPI, saveChauffeurAPI, saveLicenseInfoAPI } from '@/api/employee/chauffeurAPI'

const actions = {

  /*
  * 获取司机分页数据的action
  **/
  async fetchChauffeurAction(context, pageParams) {
    const { data } = await fetchChauffeurAPI(pageParams)
    context.commit('setChauffeurMutation', data)
  },
  /*
  *
  * 获取司机基本信息详情的action
  *  */
  async fetchChauffeurDetailAction(context, id) {
    const { data } = await fetchChauffeurDetailAPI(id)
    context.commit('setChauffeurDetailMutation', data)
    return data
  },

  /*
  *
  * 获取所有车辆的action
  *  */
  async fetchVehicleAllAction(context) {
    const { data } = await fetchVehicleAllAPI()
    context.commit('setVehicleAllMutation', data.items)
  },

  /*
  * 司机安排车辆的action
  *  */
  async bindVehicleAction(context, data) {
    await bindVehicleAPI(data)
    return '司机绑定车辆成功'
  },

  /*
  * 保存司机基本信息的action
  *  */
  async saveChauffeurAction(context, formData) {
    await saveChauffeurAPI(formData)
    return '司机信息保存成功'
  },
  /*
  *获取司机驾驶证信息的action
  *  */
  async fetchLicenseInfoAction(context, id) {
    const { data } = await fetchLicenseInfoAPI(id)
    context.commit('setLicenseInfoMutation', data)
    return data
  },
  /*
  * 保存司机驾驶证信息的action
  *  */
  async saveLicenseInfoAction(context, formData) {
    await saveLicenseInfoAPI(formData)
    return '司机驾驶证保存成功'
  }
}
const mutations = {
  /*
  * 设置司机分页数据的mutation
  **/
  setChauffeurMutation(state, { items, counts }) {
    state.chauffeurList = items
    state.counts = Number(counts)
  },

  /*
  *
  * 设置司机基本信息详情的mutation
  *  */
  setChauffeurDetailMutation(state, chauffeurDetail) {
    state.chauffeurDetail = chauffeurDetail
  },

  /*
  *
  * 设置司机驾驶证信息的mutation
  *  */
  setLicenseInfoMutation(state, licenseDetail) {
    state.licenseDetail = licenseDetail
  },

  /*
  *
  * 获取所有车辆的mutation
  *  */
  setVehicleAllMutation(state, vehicleAllList) {
    state.vehicleAllList = vehicleAllList
  },
  /*
  *
  * push一辆车辆
  *  */
  async pushVehicle(state, id) {
    const find = state.bindVehicleList.find(item => item.id === id)
    if (find) return
    const vehicle = state.vehicleAllList.find(item => item.id === id)
    if (vehicle) state.bindVehicleList.push(vehicle)
  },
  /*
  * 删除一辆车辆
  *
  *  */
  deleteVehicle(state, id) {
    state.bindVehicleList = state.bindVehicleList.filter(item => item.id !== id)
  },
  /*
  * 清空车辆的mutation
  *  */
  clearVehicle(state) {
    state.bindVehicleList = []
  }
}
const state = () => {
  return {
    chauffeurList: [],
    counts: 0,
    chauffeurDetail: {},
    licenseDetail: {},
    vehicleAllList: [],
    bindVehicleList: []
  }
}

export default {
  actions,
  mutations,
  state,
  namespaced: true
}
